import {messages} from "share/common";
import React, { Component } from 'react';
import { Input, message, Form } from 'antd';
import ScanSendService from './scan-send.service';
import errorMessage from 'share/errorMessage';

@Form.create()
class ScanSendInput extends Component {

  handlePressEnter = (e) => {
    const { sendData } = this.props;
    ScanSendService.send(e.target.value)
      .then(res => {
        if (res.data.code === '0000') {
          sendData.unshift(res.data.businessCode);
          this.props.onChange(sendData);
          message.success(res.data.msg);
        } else {
          message.error(res.data.msg);
        }
        this.props.form.resetFields();
        this.input.focus();
      })
      .catch(err => {
        this.props.form.resetFields();
        this.input.focus();
        errorMessage(err.response);
      });
  };

  componentDidMount(){
    this.input.focus();
  }

  render() {
    return (
      <section className="scan-send-enter">
        {this.props.form.getFieldDecorator('code')(
          <Input
            className="scan-send__input"
            onPressEnter={this.handlePressEnter}
            ref={input => this.input = input}
            placeholder={messages('expense-13.key26')/*请使用扫码枪/手动录入*/}
          />)
        }

      </section>
    );
  }
}

ScanSendInput.propTypes = {};

export default ScanSendInput;
